package me.hyman.testcustomview.volumeControlBar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import me.hyman.testcustomview.R;
import me.hyman.testcustomview.util.Logger;

/**
 * Created by luluteam on 2016/9/28.
 */
public class VolumeControlBar extends View {

    private int mFirstColor;
    private int mSecondColor;
    private int mItemWidth;  //每个item的高度
    private int mItemInteval;//间隔
    private Bitmap mBgImage; //中间的背景图片

    private int mCurrentCount = 8; //当前进度
    private int mItemCount;   // 总圆环数

    private Paint mPaint;
    private Rect mRect;


    public VolumeControlBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VolumeControlBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.VolumeControlBar, defStyleAttr, 0);
        int count = ta.getIndexCount();

        for(int i=0; i<count; i++) {
            int index = ta.getIndex(i);
            switch (index) {
                case R.styleable.VolumeControlBar_firstColor:
                    mFirstColor = ta.getColor(index, Color.GREEN);
                    break;
                case R.styleable.VolumeControlBar_secondColor:
                    mSecondColor = ta.getColor(index, Color.CYAN);
                    break;
                case R.styleable.VolumeControlBar_itemWidth:
                    mItemWidth = ta.getDimensionPixelSize(index, (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.VolumeControlBar_itemInterval:
                    mItemInteval = ta.getInt(index, 20);
                    break;
                case R.styleable.VolumeControlBar_itemCount:
                    mItemCount = ta.getInt(index, 20);
                    break;
                case R.styleable.VolumeControlBar_bg:
                    mBgImage = BitmapFactory.decodeResource(getResources(), ta.getResourceId(index, 0));
                    break;
            }
        }

        ta.recycle();

        mPaint = new Paint();
        mRect = new Rect();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(mItemWidth);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStyle(Paint.Style.STROKE);

        int center = getWidth() / 2;
        int radius = center - mItemWidth / 2;

        float arcAngle = (360 * 1.0f - mItemInteval * mItemCount) / mItemCount;

        RectF rectF = new RectF(center-radius, center-radius, center+radius, center+radius);

        mPaint.setColor(mFirstColor);
        for(int i=0; i<mItemCount; i++) {
            canvas.drawArc(rectF, -90+(arcAngle+mItemInteval)*i, arcAngle, false, mPaint);
        }

        mPaint.setColor(mSecondColor);
        for(int j=0; j<mCurrentCount; j++) {
            canvas.drawArc(rectF, -90+(arcAngle+mItemInteval)*j, arcAngle, false, mPaint);
        }


        /**
         *  计算内切正方形的位置
         */
        int relRadius = radius - mItemWidth / 2;// 获得内圆的半径
        /**
         * 内切正方形的距离顶部 = mCircleWidth + relRadius - √2 / 2
         */
        mRect.left = (int) (relRadius - Math.sqrt(2) * 1.0f / 2 * relRadius) + mItemWidth;
        /**
         * 内切正方形的距离左边 = mCircleWidth + relRadius - √2 / 2
         */
        mRect.top = (int) (relRadius - Math.sqrt(2) * 1.0f / 2 * relRadius) + mItemWidth;
        mRect.bottom = (int) (mRect.left + Math.sqrt(2) * relRadius);
        mRect.right = (int) (mRect.left + Math.sqrt(2) * relRadius);

        /**
         * 如果图片比较小，那么根据图片的尺寸放置到正中心
         */
        if (mBgImage.getWidth() < Math.sqrt(2) * relRadius)
        {
            mRect.left = (int) (mRect.left + Math.sqrt(2) * relRadius * 1.0f / 2 - mBgImage.getWidth() * 1.0f / 2);
            mRect.top = (int) (mRect.top + Math.sqrt(2) * relRadius * 1.0f / 2 - mBgImage.getHeight() * 1.0f / 2);
            mRect.right = (int) (mRect.left + mBgImage.getWidth());
            mRect.bottom = (int) (mRect.top + mBgImage.getHeight());

        }
        // 绘图
        canvas.drawBitmap(mBgImage, null, mRect, mPaint);
    }

    private int downY;
    private int moveStep;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        moveStep = getHeight() / 5;

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downY = (int) event.getY();
                break;

            case MotionEvent.ACTION_MOVE:

                int y = (int) event.getY();
                if(y > downY && y-moveStep >= downY) {
                    down();
                    downY = y;
                } else if(y < downY && y+moveStep <= downY) {
                    up();
                    downY = y;
                }

                break;

           /* case MotionEvent.ACTION_UP:
                int y = (int) event.getY();
                if(y > downY) {
                    down();
                } else {
                    up();
                }
                break;*/
        }
        return true;
    }

    /**
     * 上划增大音量
     */
    private void up() {
        if(mCurrentCount < mItemCount) {
            mCurrentCount++;
            postInvalidate();
        }

    }

    /**
     * 下划减小音量
     */
    private void down() {
        if(mCurrentCount > 0) {
            mCurrentCount--;
            postInvalidate();
        }
    }
}
